package com.home.admin.base;

import com.home.base.core.framework.msg.ServerResponse;
import com.home.common.annotation.CheckApiClient;
import com.home.common.constants.client.ClientConstant;
import com.home.common.framework.core.controller.BaseController;
import com.home.common.model.TreeNode;
import com.home.webapp.base.entity.BaseMenu;
import com.home.webapp.base.service.BaseMenuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * Description: 菜单 controller
 * Created by 王大宸 on 2020-10-05 23:28:38
 * Created with IntelliJ IDEA.
 */
@Controller
@RequestMapping(value = "/base/menu")
@CheckApiClient(title = "菜单管理", clientId = ClientConstant.CLIENT_WEB)
public class BaseMenuController extends BaseController<BaseMenuService, BaseMenu, String> {
    private static final Logger logger = LoggerFactory.getLogger(BaseMenuController.class);
    private static final String PATH_PREFIX = "base/menu";


    /***
     * 功能说明：跳转到菜单树界面
     *
     * @author 王大宸
     * @date 2020/10/7 15:29
     * @param
     * @return java.lang.String
     */
    @GetMapping(value = "/tree")
    public String tree() {
        return PATH_PREFIX + "/tree";
    }

    /***
     * 功能描述：跳转到信息管理界面
     *
     * @author 王大宸
     * @date 2020-10-05 23:28:38
     * @param
     * @return java.lang.String
     */
    @GetMapping(value = "/{type}/index/{parentId}")
    @RequiresPermissions("base:menu:index")
    public String index(@PathVariable("parentId") String parentId, @PathVariable("type") String type, ModelMap modelMap) {
        modelMap.put("parentId", parentId);
        if ("2".equals(type)) {
            return PATH_PREFIX + "/button/index";
        }
        return PATH_PREFIX + "/index";
    }

    /***
     * 功能描述：跳转到添加页面
     *
     * @author 王大宸
     * @date 2020-10-05 23:28:38
     * @param
     * @return java.lang.String
     */
    @GetMapping(value = "/add/{parentId}")
    @RequiresPermissions("base:menu:add")
    public String add(@PathVariable("parentId") String parentId, ModelMap modelMap) {
        modelMap.put("parentId", parentId);
        return PATH_PREFIX + "/add";
    }

    /***
     * 功能描述：跳转到修改页面
     *
     * @author 王大宸
     * @date 2020-10-05 23:28:38
     * @param id          需要修改数据的id
     * @param modelMap   modelMap
     * @return java.lang.String
     */
    @GetMapping(value = "/edit/{id}")
    @RequiresPermissions("base:menu:edit")
    public String edit(@PathVariable("id") String id, ModelMap modelMap) throws Exception {
        modelMap.put("baseMenu", baseService.getById(id));
        return PATH_PREFIX + "/edit";
    }

    /***
     * 功能描述：跳转到查看页面
     *
     * @author 王大宸
     * @date 2020-10-05 23:28:38
     * @param id          需要查看数据的id
     * @param modelMap    modelMap
     * @return java.lang.String
     */
    @GetMapping(value = "/see/{id}")
    @RequiresPermissions("base:menu:see")
    public String see(@PathVariable("id") String id, ModelMap modelMap) throws Exception {
        modelMap.put("baseMenu", baseService.getById(id));
        return PATH_PREFIX + "/see";
    }

    /***
     * 功能说明：获取菜单树(不包含按钮)
     *
     * @author 王大宸
     * @date 2020/10/7 15:42
     * @param
     * @return ServerResponse<List < Map < String, Object>>>
     */
    @GetMapping(value = "/zTree")
    @ResponseBody
    public ServerResponse<List<TreeNode>> zTree() {
        return baseService.zTree();
    }

    /***
     * 功能说明：修改菜单状态
     *
     * @author 王大宸
     * @date 2020/10/7 16:11
     * @param id        需要修改状态的菜单id
     * @param visible   状态
     * @return com.home.common.framework.msg.ServerResponse<com.home.webapp.base.entity.BaseMenu>
     */
    @PutMapping(value = "/{id}/visible/{visible}")
    @ResponseBody
    @RequiresPermissions("base:menu:edit")
    public ServerResponse<BaseMenu> visible(@PathVariable("id") String id, @PathVariable("visible") String visible) throws Exception {
        BaseMenu menu = new BaseMenu();
        menu.setId(id);
        menu.setVisible(visible);
        return ServerResponse.okData(baseService.updateInfo(menu));
    }

    /***
     * 功能说明：跳转到添加窗口
     *
     * @author 王大宸
     * @date 2020/10/7 16:12
     * @param id       菜单id
     * @param modelMap modelMap
     * @return java.lang.String
     */
    @RequestMapping(value = "/add/{id}/button", method = RequestMethod.GET)
    @RequiresPermissions("base:button:add")
    public String addButton(@PathVariable("id") String id, ModelMap modelMap) {
        modelMap.put("parentId", id);
        return PATH_PREFIX + "/button/add";
    }

    /***
     * 功能说明：跳转到按钮修改页面
     *
     * @author 王大宸
     * @date 2020/10/7 16:12
     * @param id        需要修改数据的id
     * @param modelMap  modelMap
     * @return java.lang.String
     */
    @RequestMapping(value = "/edit/{id}/button", method = RequestMethod.GET)
    @RequiresPermissions("base:button:edit")
    public String editButton(@PathVariable("id") String id, ModelMap modelMap) throws Exception {
        modelMap.put("menu", baseService.getById(id));
        return PATH_PREFIX + "/button/edit";
    }

}